<?php

class StuffDAO extends DAO {

    public static function getstuffs($uid = 0)
	{
		$uid = intval($uid);
        if (!$uid) return false;
        
        $mck = "stf:{$uid}";
        $mcconf = self::mchash("stuff", $uid);
        $ret = self::getcache($uid, $mcconf, $mck, $db, __FUNCTION__);
        if ($ret)
		{
            set_global("mc:{$mck}", $ret);
            return $ret;
        }
        $dbconf = self::dbhash($uid, "pvn_stuff", "stuff");
        $dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;
        
        $sql = "select * from {$dbconf[4]} where uid={$uid}";
		$res = self::dbquery($dbconn, $sql);
		$ret = array();
        if ($res && mysql_num_rows($res) >= 1)
		{
            while ($row = mysql_fetch_assoc($res))
            {
               
                $ret[$row["typeid"]] = $row;
            }
            unset($row);
        }
		elseif($res) $ret = array();
		else $ret = false;

        self::dbclose($dbconn);
        if ($ret === false) return false;
        
        if (self::setcache($uid, $mcconf, $mck, $ret, __FUNCTION__)) set_global("mc:{$mck}", $ret);
        return $ret;
	}

	public static function setstuff($uid = 0, $typeid = 0, $cnt = 0)
	{
		$uid = intval($uid);
		$typeid = mysql_escape_string($typeid);
		$cnt = intval($cnt);
		if (!$uid || !$typeid) return false;
    
        $ou = self::getstuffs($uid);
        if ($ou)
		{
			$mck = "stf:{$uid}";
			$mcconf = self::mchash("stuff", $uid);
			if (!self::delcache($uid, $mcconf, $mck, __FUNCTION__)) return false;
			$ou = $ou[$typeid];
		}
		elseif(!$cnt) return false;//原来没有，不用删除

		$dbconf = self::dbhash($uid, "pvn_stuff", "stuff");
		$dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
		if (!$dbconn) return false;
		
		if(!$ou) $sql = "insert into {$dbconf[4]} (uid, typeid, cnt) values('{$uid}', '{$typeid}', '{$cnt}')";
		elseif($cnt) $sql = "update {$dbconf[4]} set cnt='{$cnt}' where uid={$uid} and typeid='{$typeid}'";
		else $sql = "delete from {$dbconf[4]} where uid={$uid} and typeid='{$typeid}'";

		$ret = self::dbquery($dbconn, $sql);
		if(!$ou && $ret) $ret = self::dbid($dbconn);
		self::dbclose($dbconn);
		
		return $ret;
	}
}

?>